<?php
namespace Admin\Controller;
use Think\Controller;
class NoticeController extends Controller {
    public function index(){

        // $noticesModel=M('notices');//构建一个操作notices表的模型实例
        // $notices=$noticesModel->order('n_id ')->select();
        // $this->index=$notices;
        // dump($notices);
        $noticesModel = M('notices');
        $num = $noticesModel->order('n_id ')->count();
        $Page = new \Think\Page($num, 3);
        $Page->setConfig('header', '共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;
每页<b>3</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->link = $Page->show();
        $this->index = $noticesModel->order("n_id desc")->join('clubs  ON notices.cl_id=clubs.cl_id')->limit($Page->firstRow, 3)->select();
        $this->display();
       
    }
    public function delnotice($n_id){
    $noticesModel=M('notices'); 
    //where实在构建条件
    if (false!==$noticesModel->where("n_id=$n_id"/*必须是双引号*/)->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
        
    }


    public function addnotice()
  {
    if(IS_POST)
    {
      
      $data['n_title'] = I('post.n_title');
      $n_content = I('post.n_content');
      
      if(empty($data['n_title']))
      {
        $this->error('标题必须填写');
      }
      elseif(empty($n_content))
      {
        $this->error('内容必须填写');
      }
      else
      {
        $notice = M('notices');
        if($notice->create()){
          $notice->n_date =  date("Y-m-d H:i:s");
            if(false!==$notice->add())
                      {
                $this->success('添加成功!','index');
            }
            else{$this->error('添加失败');}
        }else{
            $this->error($notice->getError());
        }
      }
    }
    else
    { 
      $this->display();
    }    

  }

}
?>